package com.example.danangtravel.Travel;

import java.util.ArrayList;
import com.example.danangtravel.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class TravelActivity1 extends Activity {
	private ArrayList<String> listname;
	private ArrayList<Integer> listimage;
	private GridView grid;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dulich_activity);
		ImageButton im_home = (ImageButton)findViewById(R.id.im_homdulich);
		im_home.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		preparelist();
		grid =(GridView)findViewById(R.id.grid_dulich1);
		grid.setAdapter(new  gridAdapter(this, listname, listimage));
		grid.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent i = new Intent(getApplicationContext(), Travel_Item.class);
				i.putExtra("id", arg2 + 1);
				startActivity(i);
			}
		});
	}

	public void preparelist() {
		listname = new ArrayList<String>();
		listname.add("Bà Nà");
		listname.add("Bãi Bụt");
		listname.add("Bãi Rạng");
		listname.add("Suối Hoa");
		listname.add("Suối Lương");
		listname.add("Ngầm Đôi");
		listname.add("Non Nước");
		listname.add("Cù Lao Chàm");
		listname.add("ChùaLinhỨng");

		listimage = new ArrayList<Integer>();
		listimage.add(R.drawable.du1);
		listimage.add(R.drawable.du2);
		listimage.add(R.drawable.du3);
		listimage.add(R.drawable.du4);
		listimage.add(R.drawable.du5);
		listimage.add(R.drawable.du6);
		listimage.add(R.drawable.du7);
		listimage.add(R.drawable.du8);
		listimage.add(R.drawable.du9);

	}

	public class gridAdapter extends BaseAdapter {
		private ArrayList<String> listname1;
		private ArrayList<Integer> listimage1;
		private Activity activity1;

		public gridAdapter(Activity activity1, ArrayList<String> listname1,
				ArrayList<Integer> listimage1) {
			super();
			this.activity1 = activity1;
			this.listimage1 = listimage1;
			this.listname1 = listname1;
		}

		public int getCount() {
			// TODO Auto-generated method stub
			return listname1.size();
		}

		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return listname1.get(arg0);
		}

		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

	
			public class ViewHolder
		    {
		        public ImageView imgView;
		        public TextView txtView;
		    }
		
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			ViewHolder view;
			LayoutInflater inflater = activity1.getLayoutInflater();
			
			if (arg1 == null){
				view = new ViewHolder();
				arg1 = inflater.inflate(R.layout.dulich_rowgrid, null);
				view.imgView = (ImageView)arg1.findViewById(R.id.im_dulich1);
				view.txtView = (TextView)arg1.findViewById(R.id.tv_dulich1);
				
				arg1.setTag(view);
			}
			else {
				view =(ViewHolder)arg1.getTag();
			}
			view.imgView.setImageResource(listimage1.get(arg0));
			view.txtView.setText(listname1.get(arg0));
					
			return arg1;
		}

	}
}
